package com.yifeng.repo.controller.rocketmq.message.produce;

import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

/**
 * Created by daibing on 2023/8/5.
 */
public class OrderMessageQueueSelector implements MessageQueueSelector {

    @Override
    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
        // 优先按照arg排序
        if (arg != null) {
            return mqs.get((arg.hashCode() & Integer.MAX_VALUE) % mqs.size());
        }

        // 同一个tag的消息进入同一个队列，就可以保证tag内顺序消费
        return mqs.get((msg.getTags().hashCode() & Integer.MAX_VALUE) % mqs.size());
    }

}
